{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 词级别可解释性分析\n",
    "本项目提供模型的词级别可解释性分析，包括LIME、Integrated Gradient、GradShap 三种分析方法，支持分析微调后模型的预测结果，开发者可以通过更改**数据目录**和**模型目录**在自己的任务中使用此项目进行数据分析。\n",
    "\n",
    "![image](https://user-images.githubusercontent.com/63761690/195086276-6ee16e96-4ec3-4a0f-821f-37546d21746b.png)\n",
    " \n",
    "\n",
    "## 1.导入Python模块与参数配置\n",
    "首先我们导入必要的导入必要python模块和设置配置参数，词级别可解释性分析算法支持三种待分析的文本 `INTERPRETER_FILE` 数据文件格式：\n",
    "\n",
    "**格式一：包括文本、标签、预测结果**\n",
    "```text\n",
    "<文本>'\\t'<标签>'\\t'<预测结果>\n",
    "...\n",
    "```\n",
    "\n",
    "**格式二：包括文本、标签**\n",
    "```text\n",
    "<文本>'\\t'<标签>\n",
    "...\n",
    "```\n",
    "\n",
    "**格式三：只包括文本**\n",
    "```text\n",
    "<文本>\n",
    "...\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import functools\n",
    "import random\n",
    "import os\n",
    "import argparse\n",
    "\n",
    "import jieba\n",
    "import numpy as np \n",
    "from trustai.interpretation import VisualizationTextRecord\n",
    "from trustai.interpretation import get_word_offset\n",
    "import paddle\n",
    "from paddle.io import DataLoader, BatchSampler\n",
    "from paddlenlp.data import DataCollatorWithPadding\n",
    "from paddlenlp.datasets import load_dataset\n",
    "from paddlenlp.transformers import AutoModelForSequenceClassification, AutoTokenizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from trustai.interpretation import VisualizationTextRecord\n",
    "from trustai.interpretation import get_word_offset\n",
    "import paddle\n",
    "from paddle.io import DataLoader, BatchSampler\n",
    "from paddlenlp.data import DataCollatorWithPadding\n",
    "from paddlenlp.datasets import load_dataset\n",
    "from paddlenlp.transformers import AutoModelForSequenceClassification, AutoTokenizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 预先定义配置参数\n",
    "\n",
    "# 运行环境，可选\"cpu\",\"gpu\",\"gpu:x\"(x为gpu编号)\n",
    "DEVICE = \"gpu\"\n",
    "# 数据路径\n",
    "DATASET_DIR = \"../data\" \n",
    "# 训练模型保存路径\n",
    "PARAM_PATH = \"../checkpoint/\" \n",
    "# tokenizer使用的最大序列长度，ERNIE模型最大不能超过2048。请根据文本长度选择，通常推荐128、256或512，若出现显存不足，请适当调低这一参数\n",
    "MAX_LENGTH = 128 \n",
    "# 批处理大小，请结合显存情况进行调整，若出现显存不足，请适当调低这一参数\n",
    "BATCH_SIZE = 1 \n",
    "# 待分析解释的数据\n",
    "INTERPRETER_FILE = \"bad_case.txt\"\n",
    "# 可选 \"ig\",\"lime\",\"grad\" ,可以根据实际任务效果选择解释器\n",
    "# \"grad\":GradShap方法依赖interpretdl\n",
    "# !pip install interpretdl\n",
    "INTERPRETER = \"ig\"\n",
    "# 分析句子中TOP K关键词，K值\n",
    "KEY_WORDS_NUM = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_local_dataset(path):\n",
    "    \"\"\"\n",
    "    Read dataset file\n",
    "    \"\"\"\n",
    "    with open(path, 'r', encoding='utf-8') as f:\n",
    "        for line in f:\n",
    "            items = line.strip().split('\\t')\n",
    "            if items[0] == 'Text':\n",
    "                continue\n",
    "            items[0] = items[0][:MAX_LENGTH-2]\n",
    "            if len(items) == 3:\n",
    "                yield {'text': items[0], 'label': items[1], 'predict': items[2]}\n",
    "            elif len(items) == 2:\n",
    "                yield {'text': items[0], 'label': items[1], 'predict': ''}\n",
    "            elif len(items) == 1:\n",
    "                yield {'text': items[0], 'label': '', 'predict': ''}\n",
    "            else:\n",
    "                raise ValueError(\"{} should be in fixed format.\".format(path))\n",
    "\n",
    "def preprocess_function(examples, tokenizer, max_seq_length):\n",
    "    \"\"\"\n",
    "    Preprocess dataset\n",
    "    \"\"\"\n",
    "    result = tokenizer(text=examples[\"text\"], max_seq_len=max_seq_length)\n",
    "    return result\n",
    "\n",
    "class LocalDataCollatorWithPadding(DataCollatorWithPadding):\n",
    "    \"\"\"\n",
    "    Convert the  result of DataCollatorWithPadding from dict dictionary to a list\n",
    "    \"\"\"\n",
    "\n",
    "    def __call__(self, features):\n",
    "        batch = super().__call__(features)\n",
    "        batch = list(batch.values())\n",
    "        return batch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m[2022-10-11 12:17:29,041] [    INFO]\u001b[0m - We are using <class 'paddlenlp.transformers.ernie.modeling.ErnieForSequenceClassification'> to load '/workspace/PaddleNLP/applications/text_classification/multi_class/checkpoint/'.\u001b[0m\n",
      "W1011 12:17:29.044690 79080 gpu_resources.cc:61] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 11.2\n",
      "W1011 12:17:29.051118 79080 gpu_resources.cc:91] device: 0, cuDNN Version: 8.1.\n",
      "\u001b[32m[2022-10-11 12:17:32,517] [    INFO]\u001b[0m - We are using <class 'paddlenlp.transformers.ernie.tokenizer.ErnieTokenizer'> to load '/workspace/PaddleNLP/applications/text_classification/multi_class/checkpoint/'.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "paddle.set_device(DEVICE)\n",
    "\n",
    "# Define model & tokenizer\n",
    "if os.path.exists(PARAM_PATH):\n",
    "    model = AutoModelForSequenceClassification.from_pretrained(PARAM_PATH)\n",
    "    tokenizer = AutoTokenizer.from_pretrained(PARAM_PATH)\n",
    "else:\n",
    "    raise ValueError(\"The {} should exist.\".format(PARAM_PATH))\n",
    "\n",
    "# Prepare & preprocess dataset\n",
    "interpret_path = os.path.join(DATASET_DIR, INTERPRETER_FILE)\n",
    "\n",
    "\n",
    "interpret_ds = load_dataset(read_local_dataset, path=interpret_path, lazy=False)\n",
    "trans_func = functools.partial(preprocess_function,\n",
    "                                tokenizer=tokenizer,\n",
    "                                max_seq_length=MAX_LENGTH)\n",
    "\n",
    "interpret_ds = interpret_ds.map(trans_func)\n",
    "\n",
    "# Batchify dataset\n",
    "collate_fn = LocalDataCollatorWithPadding(tokenizer)\n",
    "interpret_batch_sampler = BatchSampler(interpret_ds,\n",
    "                                    batch_size=BATCH_SIZE,\n",
    "                                    shuffle=False)\n",
    "interpret_data_loader = DataLoader(dataset=interpret_ds,\n",
    "                                batch_sampler=interpret_batch_sampler,\n",
    "                                collate_fn=collate_fn)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start token level interpretion, it will take some time...\n",
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache /tmp/jieba.cache\n",
      "Loading model cost 1.005 seconds.\n",
      "Prefix dict has been built successfully.\n",
      "Start word level alignment, it will take some time...\n"
     ]
    }
   ],
   "source": [
    "# Init an interpreter\n",
    "if INTERPRETER == 'ig':\n",
    "    from trustai.interpretation.token_level import IntGradInterpreter\n",
    "    interpreter = IntGradInterpreter(model)\n",
    "elif INTERPRETER == 'lime':\n",
    "    from trustai.interpretation.token_level import LIMEInterpreter\n",
    "    interpreter = LIMEInterpreter(model, unk_id=tokenizer.convert_tokens_to_ids('[UNK]'), pad_id=tokenizer.convert_tokens_to_ids('[PAD]'))\n",
    "else:\n",
    "    from trustai.interpretation.token_level import GradShapInterpreter\n",
    "    interpreter = GradShapInterpreter(model)\n",
    "\n",
    "# Use interpreter to get the importance scores for all data\n",
    "print(\"Start token level interpretion, it will take some time...\")\n",
    "analysis_result = []\n",
    "for batch in interpret_data_loader:\n",
    "    analysis_result += interpreter(tuple(batch))\n",
    "\n",
    "# Add CLS and SEP tags to both original text and standard splited tokens\n",
    "contexts = []\n",
    "words = []\n",
    "for i in range(len(interpret_ds)):\n",
    "    text = interpret_ds.data[i][\"text\"]\n",
    "    contexts.append(\"[CLS]\" + text + \"[SEP]\")\n",
    "    words.append([\"[CLS]\"] + list(jieba.cut(text)) + [\"[SEP]\"])\n",
    "\n",
    "# Get the offset map of tokenized tokens and standard splited tokens\n",
    "print(\"Start word level alignment, it will take some time...\")\n",
    "ori_offset_maps = []\n",
    "word_offset_maps = []\n",
    "for i in range(len(contexts)):\n",
    "    ori_offset_maps.append(tokenizer.get_offset_mapping(contexts[i]))\n",
    "    word_offset_maps.append(get_word_offset(contexts[i], words[i]))\n",
    "\n",
    "align_res = interpreter.alignment(analysis_result, contexts, words, word_offset_maps, ori_offset_maps, special_tokens=[\"[CLS]\", '[SEP]'],rationale_num=KEY_WORDS_NUM)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "class Visualization(VisualizationTextRecord):\n",
    "\n",
    "    def __init__(self, interpret_res, true_label=None, pred_label=None, words=None):\n",
    "        if words is not None:\n",
    "            self.words = words\n",
    "        else:\n",
    "            self.words = interpret_res.words\n",
    "        self.pred_label = pred_label if pred_label is not None else ''\n",
    "        self.true_label = true_label if true_label is not None else ''\n",
    "        self.key_words = \" \".join(set(interpret_res.rationale_tokens))\n",
    "        word_attributions = interpret_res.word_attributions\n",
    "        _max = max(word_attributions)\n",
    "        _min = min(word_attributions)\n",
    "        self.word_attributions = [(word_imp - _min) / (_max - _min) for word_imp in word_attributions]\n",
    "\n",
    "    def record_html(self):\n",
    "        \"\"\"change all informations to html\"\"\"\n",
    "        return \"\".join([\n",
    "            \"<tr>\",\n",
    "            self._format_class(self.true_label),\n",
    "            self._format_class(self.pred_label),\n",
    "            self._format_class(self.key_words),\n",
    "            self._format_word_attributions(),\n",
    "            \"<tr>\",\n",
    "        ])\n",
    "    def _format_class(self, label):\n",
    "        return '<td align=\"center\"><text style=\"padding-right:2em\"><b>{label}</b></text></td>'.format(label=label)\n",
    "\n",
    "def visualize_text(text_records):\n",
    "    \"\"\"visualize text\"\"\"\n",
    "    html = [\"<table width: 100%, align : center>\"]\n",
    "    rows = [\"<tr><th>Label</th>\"\n",
    "            \"<th>Prediction</th>\"\n",
    "            \"<th>Key words</th>\"\n",
    "            \"<th>Important visualization</th>\"]\n",
    "    for record in text_records:\n",
    "        rows.append(record.record_html())\n",
    "    html.append(\"\".join(rows))\n",
    "    html.append(\"</table>\")\n",
    "    html = HTML(\"\".join(html))\n",
    "    display(html)\n",
    "    return html.data\n",
    "\n",
    "\n",
    "def visualize(interpret_res, ds):\n",
    "    records = []\n",
    "    for i in range(len(interpret_res)):\n",
    "        records.append(Visualization(interpret_res[i], true_label=ds.data[i][\"label\"], pred_label=ds.data[i][\"predict\"]))\n",
    "    html = visualize_text(records)\n",
    "    return html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table width: 100%, align : center><tr><th>Label</th><th>Prediction</th><th>Key words</th><th>Important visualization</th><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>其他</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>注意事项</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>月 服用 请问 的 可以</b></text></td><td><mark style=\"background-color: hsl(120, 75%, 92%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 92%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 您好                        </font></mark><mark style=\"background-color: hsl(120, 75%, 91%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> ，                        </font></mark><mark style=\"background-color: hsl(120, 75%, 86%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 请问                        </font></mark><mark style=\"background-color: hsl(120, 75%, 88%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 一岁                        </font></mark><mark style=\"background-color: hsl(120, 75%, 87%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 三个                        </font></mark><mark style=\"background-color: hsl(120, 75%, 85%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 月                        </font></mark><mark style=\"background-color: hsl(120, 75%, 85%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 的                        </font></mark><mark style=\"background-color: hsl(120, 75%, 88%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 孩子                        </font></mark><mark style=\"background-color: hsl(120, 75%, 76%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 可以                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 服用                        </font></mark><mark style=\"background-color: hsl(120, 75%, 93%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 复方                        </font></mark><mark style=\"background-color: hsl(120, 75%, 91%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 锌                        </font></mark><mark style=\"background-color: hsl(120, 75%, 90%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 布                        </font></mark><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 颗粒                        </font></mark><mark style=\"background-color: hsl(120, 75%, 87%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 吗                        </font></mark><mark style=\"background-color: hsl(120, 75%, 89%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> ？                        </font></mark><mark style=\"background-color: hsl(120, 75%, 82%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>其他</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>就医建议</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>输卵管 基本 检查 粘连 的</b></text></td><td><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 94%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 输卵管                        </font></mark><mark style=\"background-color: hsl(120, 75%, 99%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 粘连                        </font></mark><mark style=\"background-color: hsl(120, 75%, 94%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 的                        </font></mark><mark style=\"background-color: hsl(120, 75%, 97%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 基本                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 检查                        </font></mark><mark style=\"background-color: hsl(120, 75%, 97%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>其他</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>病情诊断</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>胎动 么 ？ 是 会</b></text></td><td><mark style=\"background-color: hsl(120, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 会                        </font></mark><mark style=\"background-color: hsl(120, 75%, 86%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 是                        </font></mark><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 胎动                        </font></mark><mark style=\"background-color: hsl(120, 75%, 85%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 么                        </font></mark><mark style=\"background-color: hsl(120, 75%, 79%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> ？                        </font></mark><mark style=\"background-color: hsl(120, 75%, 84%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>其他</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>病情诊断</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>这是 经常 干呕 了 生病</b></text></td><td><mark style=\"background-color: hsl(120, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 92%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 经常                        </font></mark><mark style=\"background-color: hsl(120, 75%, 88%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 干呕                        </font></mark><mark style=\"background-color: hsl(120, 75%, 96%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 恶心                        </font></mark><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> ，                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 这是                        </font></mark><mark style=\"background-color: hsl(120, 75%, 88%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 生病                        </font></mark><mark style=\"background-color: hsl(120, 75%, 87%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 了                        </font></mark><mark style=\"background-color: hsl(120, 75%, 96%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 吗                        </font></mark><mark style=\"background-color: hsl(120, 75%, 95%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>就医建议</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>治疗方案</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>治 治疗 菏泽 怎么 白癜风</b></text></td><td><mark style=\"background-color: hsl(120, 75%, 93%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 91%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 菏泽                        </font></mark><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 哪个                        </font></mark><mark style=\"background-color: hsl(120, 75%, 96%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 医院                        </font></mark><mark style=\"background-color: hsl(120, 75%, 92%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 治疗                        </font></mark><mark style=\"background-color: hsl(120, 75%, 91%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 白癜风                        </font></mark><mark style=\"background-color: hsl(120, 75%, 96%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 比较                        </font></mark><mark style=\"background-color: hsl(120, 75%, 95%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 好                        </font></mark><mark style=\"background-color: hsl(120, 75%, 93%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> ?                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 怎么                        </font></mark><mark style=\"background-color: hsl(120, 75%, 87%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 治                        </font></mark><mark style=\"background-color: hsl(120, 75%, 92%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 好                        </font></mark><mark style=\"background-color: hsl(120, 75%, 93%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>其他</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>后果表述</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>左旋 不良反应 吃 的 肉碱</b></text></td><td><mark style=\"background-color: hsl(120, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 97%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 吃                        </font></mark><mark style=\"background-color: hsl(120, 75%, 96%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 左旋                        </font></mark><mark style=\"background-color: hsl(120, 75%, 96%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 肉碱                        </font></mark><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 后                        </font></mark><mark style=\"background-color: hsl(120, 75%, 95%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 的                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 不良反应                        </font></mark><mark style=\"background-color: hsl(120, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>注意事项</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>其他</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>上 出血 吗 做爱 环后</b></text></td><td><mark style=\"background-color: hsl(120, 75%, 95%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 89%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 上                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 环后                        </font></mark><mark style=\"background-color: hsl(120, 75%, 85%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 出血                        </font></mark><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 可以                        </font></mark><mark style=\"background-color: hsl(120, 75%, 89%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 做爱                        </font></mark><mark style=\"background-color: hsl(120, 75%, 96%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 吗                        </font></mark><mark style=\"background-color: hsl(120, 75%, 86%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>病情诊断</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>病因分析</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>感冒 了 呀 怎么 会</b></text></td><td><mark style=\"background-color: hsl(120, 75%, 96%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 97%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 孩子                        </font></mark><mark style=\"background-color: hsl(120, 75%, 88%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 感冒                        </font></mark><mark style=\"background-color: hsl(120, 75%, 89%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 了                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 怎么                        </font></mark><mark style=\"background-color: hsl(120, 75%, 84%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 会                        </font></mark><mark style=\"background-color: hsl(120, 75%, 96%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 喘息                        </font></mark><mark style=\"background-color: hsl(120, 75%, 93%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 呀                        </font></mark><mark style=\"background-color: hsl(120, 75%, 94%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> ？                        </font></mark><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>其他</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>治疗方案</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>孕 周 21</b></text></td><td><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 孕                        </font></mark><mark style=\"background-color: hsl(120, 75%, 88%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 21                        </font></mark><mark style=\"background-color: hsl(120, 75%, 97%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 周                        </font></mark><mark style=\"background-color: hsl(120, 75%, 92%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>其他</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>指标解读</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>谱 心肌 意义 酶 ？</b></text></td><td><mark style=\"background-color: hsl(120, 75%, 83%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 心肌                        </font></mark><mark style=\"background-color: hsl(120, 75%, 83%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 酶                        </font></mark><mark style=\"background-color: hsl(120, 75%, 76%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 谱                        </font></mark><mark style=\"background-color: hsl(120, 75%, 85%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 五项                        </font></mark><mark style=\"background-color: hsl(120, 75%, 82%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 意义                        </font></mark><mark style=\"background-color: hsl(120, 75%, 79%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> ？                        </font></mark><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr><tr><td align=\"center\"><text style=\"padding-right:2em\"><b>病情诊断</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>其他</b></text></td><td align=\"center\"><text style=\"padding-right:2em\"><b>家长 判断 吃 吃饱 怎么</b></text></td><td><mark style=\"background-color: hsl(120, 75%, 86%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [CLS]                        </font></mark><mark style=\"background-color: hsl(120, 75%, 70%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 家长                        </font></mark><mark style=\"background-color: hsl(120, 75%, 88%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 怎么                        </font></mark><mark style=\"background-color: hsl(120, 75%, 88%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 判断                        </font></mark><mark style=\"background-color: hsl(120, 75%, 89%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 孩子                        </font></mark><mark style=\"background-color: hsl(120, 75%, 80%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 吃饱                        </font></mark><mark style=\"background-color: hsl(120, 75%, 97%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 了                        </font></mark><mark style=\"background-color: hsl(120, 75%, 94%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 呢                        </font></mark><mark style=\"background-color: hsl(0, 75%, 100%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> ？                        </font></mark><mark style=\"background-color: hsl(120, 75%, 95%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 怎么                        </font></mark><mark style=\"background-color: hsl(120, 75%, 92%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 都                        </font></mark><mark style=\"background-color: hsl(120, 75%, 95%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 不肯                        </font></mark><mark style=\"background-color: hsl(120, 75%, 87%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 吃                        </font></mark><mark style=\"background-color: hsl(120, 75%, 93%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 了                        </font></mark><mark style=\"background-color: hsl(120, 75%, 95%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 就是                        </font></mark><mark style=\"background-color: hsl(120, 75%, 94%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 饱                        </font></mark><mark style=\"background-color: hsl(120, 75%, 91%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> 了                        </font></mark><mark style=\"background-color: hsl(120, 75%, 87%); opacity:1.0;                         line-height:1.75\"><font color=\"black\"> [SEP]                        </font></mark></td><tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# process for vbisualize\n",
    "html = visualize(align_res, interpret_ds)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.13 64-bit",
   "metadata": {
    "interpreter": {
     "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
    }
   },
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13-final"
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
